// pages/settlement/settlement.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    //* 应付总额
    TotalAmount: 0,
    //* 商品数据
    commodity: [],
    //* 用户数据
    UserArr: [],
    cartId: [],
    UserId: [],
    // 支付弹框
    isShowConfirm: false,
    // 支付密码
    PaymentPassword: '',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let that = this;
    wx.getStorage({
      key: 'token',
      success(res) {
        wx.request({
          header: {
            'content-type': 'application/x-www-form-urlencoded'
          },
          url: 'http://47.104.142.216/TP/public/index.php/index/User/VerificationToken', //本地设置不校验合法域名
          data: {
            token: res.data
          },
          method: 'POST',
          dataType: 'JSON',
          success: function (res) {
            let keys = JSON.parse(res.data)
            if (keys.errorCode == 10009) {
              wx.showToast({
                title: '您还未登录，请先登录',
                icon: 'none',
                duration: 1500
              })
              //TODO 跳转
              wx.redirectTo({
                url: '../Login/Login',
              })
            } else if (keys.errorCode == 10017) {
              wx.removeStorage({
                key: 'token',
                success(res) {
                  // console.log(res)
                }
              })
              wx.showToast({
                title: '您还未登录，请先登录',
                icon: 'none',
                duration: 1500
              })
              //TODO 跳转
              wx.redirectTo({
                url: '../Login/Login',
              })
            } else {
              if (options.cartId == '' && options.TotalAmount == '' && options.UserId == '') {
                wx.showToast({
                  title: '请先在购物车选择商品',
                  icon: 'none',
                  duration: 1500
                })
                wx.switchTab({
                  url: '../ShoppingCart/ShoppingCart'
                })
              } else {
                that.setData({
                  TotalAmount: options.TotalAmount,
                  cartId: options.cartId,
                  UserId: options.UserId,
                })
                wx.request({
                  header: {
                    'content-type': 'application/x-www-form-urlencoded'
                  },
                  url: 'http://47.104.142.216/TP/public/index.php/index/Cart/getPay', //本地设置不校验合法域名
                  data: {
                    cartId: JSON.stringify(options.cartId),
                    UserId: options.UserId
                  },
                  method: 'POST',
                  dataType: 'JSON',
                  success: function (res) {
                    let keys = JSON.parse(res.data)
                    that.setData({
                      UserArr: keys[1],
                      commodity: keys[0]
                    })
                  }
                })
              }
            }
          }
        })
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },

  /**
   * 点击返回购物车
   */
  goShopping: function () {
    let that = this;
    wx.showModal({
      title: '提示',
      content: '确定要要返回购物车吗?这将结束付款',
      success: function (sm) {
        if (sm.confirm) {
          //点击确定
          wx.switchTab({
            url: '../ShoppingCart/ShoppingCart'
          })
        } else if (sm.cancel) {
          //点击取消
        }
      }
    })
  },

  /**
   * 点击弹出支付框
   */
  Purchase: function (e) {
    let that = this;
    that.setData({
      isShowConfirm: true,
    })

  },

  /**
   * 输入支付密码
   */
  setValue: function (e) {
    this.setData({
      PaymentPassword: e.detail.value
    })
  },

  /**
   * 取消支付
   */
  cancel: function () {
    var that = this
    that.setData({
      isShowConfirm: false,
    })
  },

  /**
   * 确认支付
   */
  confirmAcceptance: function () {
    var that = this;
    wx.request({
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      url: 'http://47.104.142.216/TP/public/index.php/index/Cart/Purchase', //本地设置不校验合法域名
      data: {
        AppCartId:that.data.cartId,
        UserId:that.data.UserId,
        TotalAmount: that.data.TotalAmount,
        UserPay: that.data.PaymentPassword,
      },
      method: 'POST',
      dataType: 'JSON',
      success: function (res) {
        let keys = JSON.parse(res.data)
        if (keys.errorCode == 10019) {
          wx.showToast({
            title: '账户余额不足!',
            icon: 'none',
            duration: 1500
          })
          that.setData({
            isShowConfirm: false,
          })
        } else if(keys.errorCode == 10020) {
          wx.showToast({
            title: '下单付款成功，请前往个人中心查看',
            icon: 'none',
            duration: 1500
          })
          that.setData({
            isShowConfirm: false,
          })
          wx.switchTab({
            url: '../ShoppingCart/ShoppingCart'
          })
        } else if(keys.errorCode == 10021){
          wx.showToast({
            title: '支付密码不正确',
            icon: 'none',
            duration: 1500
          })
        }
      }
    })
  }
})